﻿using System;
using System.Windows.Forms;
using PerCiGal.WinForm.Controls;
using PerCiGal.WinForm.Collections;
using System.Threading;

namespace PerCiGal.WinForm
{
    public partial class MainForm : Form
    {
        private const string PcgText = "PerCiGal v{0}";

        public MainForm()
        {
            InitializeComponent();

            Text = string.Format(PcgText, Common.ProductVersion.Version);
        }

        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        private void ButtonCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void ButtonCollectionsClick(object sender, EventArgs e)
        {
            Opacity = 0.70;

            var delegato = new EndOperationDelegate(LoadCollection);

            var collectionsManagerForm = new CollectionsManagerForm();

            collectionsManagerForm.EndOperationDelegate = delegato;

            ThreadPool.QueueUserWorkItem(new WaitCallback(OpenForm), collectionsManagerForm);
        }


        public void LoadCollection(object state)
        {
            Opacity = 1;

            if(state != null)
            {
               //TODO
            }
        }

        private void OpenForm(object state)
        {
            try
            {
                if (this.InvokeRequired)
                {
                    var d = new EndOperationDelegate(OpenForm);
                    this.Invoke(d, state);
                }
                else
                {
                    ((Form)state).ShowDialog(this);
                }
            }
            catch (Exception error)
            {
                throw;
            }
        }
    }
}
